// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.Aggregations;

internal sealed class PercentileRanksAggregationConverter : JsonConverter<PercentileRanksAggregation>
{
	public override PercentileRanksAggregation Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
			throw new JsonException("Unexpected JSON detected.");
		reader.Read();
		var aggName = reader.GetString();
		if (aggName != "percentile_ranks")
			throw new JsonException("Unexpected JSON detected.");
		var agg = new PercentileRanksAggregation(aggName);
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				if (reader.ValueTextEquals("field"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Field?>(ref reader, options);
					if (value is not null)
					{
						agg.Field = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("format"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<string?>(ref reader, options);
					if (value is not null)
					{
						agg.Format = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("hdr"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.HdrMethod?>(ref reader, options);
					if (value is not null)
					{
						agg.Hdr = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("missing"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<FieldValue?>(ref reader, options);
					if (value is not null)
					{
						agg.Missing = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("script"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Script?>(ref reader, options);
					if (value is not null)
					{
						agg.Script = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("tdigest"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.TDigest?>(ref reader, options);
					if (value is not null)
					{
						agg.TDigest = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("values"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<ICollection<double>?>(ref reader, options);
					if (value is not null)
					{
						agg.Values = value;
					}

					continue;
				}
			}
		}

		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				if (reader.ValueTextEquals("meta"))
				{
					var value = JsonSerializer.Deserialize<Dictionary<string, object>>(ref reader, options);
					if (value is not null)
					{
						agg.Meta = value;
					}

					continue;
				}
			}
		}

		return agg;
	}

	public override void Write(Utf8JsonWriter writer, PercentileRanksAggregation value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		writer.WritePropertyName("percentile_ranks");
		writer.WriteStartObject();
		if (value.Field is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, value.Field, options);
		}

		if (!string.IsNullOrEmpty(value.Format))
		{
			writer.WritePropertyName("format");
			writer.WriteStringValue(value.Format);
		}

		if (value.Hdr is not null)
		{
			writer.WritePropertyName("hdr");
			JsonSerializer.Serialize(writer, value.Hdr, options);
		}

		if (value.Missing is not null)
		{
			writer.WritePropertyName("missing");
			JsonSerializer.Serialize(writer, value.Missing, options);
		}

		if (value.Script is not null)
		{
			writer.WritePropertyName("script");
			JsonSerializer.Serialize(writer, value.Script, options);
		}

		if (value.TDigest is not null)
		{
			writer.WritePropertyName("tdigest");
			JsonSerializer.Serialize(writer, value.TDigest, options);
		}

		if (value.Values is not null)
		{
			writer.WritePropertyName("values");
			JsonSerializer.Serialize(writer, value.Values, options);
		}

		writer.WriteEndObject();
		if (value.Meta is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, value.Meta, options);
		}

		writer.WriteEndObject();
	}
}

[JsonConverter(typeof(PercentileRanksAggregationConverter))]
public sealed partial class PercentileRanksAggregation : SearchAggregation
{
	public PercentileRanksAggregation(string name, Field field) : this(name) => Field = field;
	public PercentileRanksAggregation(string name) => Name = name;

	internal PercentileRanksAggregation()
	{
	}

	public Elastic.Clients.Elasticsearch.Serverless.Field? Field { get; set; }
	public string? Format { get; set; }

	/// <summary>
	/// <para>Uses the alternative High Dynamic Range Histogram algorithm to calculate percentile ranks.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.Serverless.Aggregations.HdrMethod? Hdr { get; set; }
	public IDictionary<string, object>? Meta { get; set; }
	public FieldValue? Missing { get; set; }
	override public string? Name { get; internal set; }
	public Elastic.Clients.Elasticsearch.Serverless.Script? Script { get; set; }

	/// <summary>
	/// <para>Sets parameters for the default TDigest algorithm used to calculate percentile ranks.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.Serverless.Aggregations.TDigest? TDigest { get; set; }

	/// <summary>
	/// <para>An array of values for which to calculate the percentile ranks.</para>
	/// </summary>
	public ICollection<double>? Values { get; set; }
}

public sealed partial class PercentileRanksAggregationDescriptor<TDocument> : SerializableDescriptor<PercentileRanksAggregationDescriptor<TDocument>>
{
	internal PercentileRanksAggregationDescriptor(Action<PercentileRanksAggregationDescriptor<TDocument>> configure) => configure.Invoke(this);

	public PercentileRanksAggregationDescriptor() : base()
	{
	}

	private Elastic.Clients.Elasticsearch.Serverless.Field? FieldValue { get; set; }
	private string? FormatValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.HdrMethod? HdrValue { get; set; }
	private HdrMethodDescriptor HdrDescriptor { get; set; }
	private Action<HdrMethodDescriptor> HdrDescriptorAction { get; set; }
	private IDictionary<string, object>? MetaValue { get; set; }
	private FieldValue? MissingValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Script? ScriptValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.TDigest? TDigestValue { get; set; }
	private TDigestDescriptor TDigestDescriptor { get; set; }
	private Action<TDigestDescriptor> TDigestDescriptorAction { get; set; }
	private ICollection<double>? ValuesValue { get; set; }

	public PercentileRanksAggregationDescriptor<TDocument> Field(Elastic.Clients.Elasticsearch.Serverless.Field? field)
	{
		FieldValue = field;
		return Self;
	}

	public PercentileRanksAggregationDescriptor<TDocument> Field<TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	public PercentileRanksAggregationDescriptor<TDocument> Format(string? format)
	{
		FormatValue = format;
		return Self;
	}

	/// <summary>
	/// <para>Uses the alternative High Dynamic Range Histogram algorithm to calculate percentile ranks.</para>
	/// </summary>
	public PercentileRanksAggregationDescriptor<TDocument> Hdr(Elastic.Clients.Elasticsearch.Serverless.Aggregations.HdrMethod? hdr)
	{
		HdrDescriptor = null;
		HdrDescriptorAction = null;
		HdrValue = hdr;
		return Self;
	}

	public PercentileRanksAggregationDescriptor<TDocument> Hdr(HdrMethodDescriptor descriptor)
	{
		HdrValue = null;
		HdrDescriptorAction = null;
		HdrDescriptor = descriptor;
		return Self;
	}

	public PercentileRanksAggregationDescriptor<TDocument> Hdr(Action<HdrMethodDescriptor> configure)
	{
		HdrValue = null;
		HdrDescriptor = null;
		HdrDescriptorAction = configure;
		return Self;
	}

	public PercentileRanksAggregationDescriptor<TDocument> Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
	{
		MetaValue = selector?.Invoke(new FluentDictionary<string, object>());
		return Self;
	}

	public PercentileRanksAggregationDescriptor<TDocument> Missing(FieldValue? missing)
	{
		MissingValue = missing;
		return Self;
	}

	public PercentileRanksAggregationDescriptor<TDocument> Script(Elastic.Clients.Elasticsearch.Serverless.Script? script)
	{
		ScriptValue = script;
		return Self;
	}

	/// <summary>
	/// <para>Sets parameters for the default TDigest algorithm used to calculate percentile ranks.</para>
	/// </summary>
	public PercentileRanksAggregationDescriptor<TDocument> TDigest(Elastic.Clients.Elasticsearch.Serverless.Aggregations.TDigest? tDigest)
	{
		TDigestDescriptor = null;
		TDigestDescriptorAction = null;
		TDigestValue = tDigest;
		return Self;
	}

	public PercentileRanksAggregationDescriptor<TDocument> TDigest(TDigestDescriptor descriptor)
	{
		TDigestValue = null;
		TDigestDescriptorAction = null;
		TDigestDescriptor = descriptor;
		return Self;
	}

	public PercentileRanksAggregationDescriptor<TDocument> TDigest(Action<TDigestDescriptor> configure)
	{
		TDigestValue = null;
		TDigestDescriptor = null;
		TDigestDescriptorAction = configure;
		return Self;
	}

	/// <summary>
	/// <para>An array of values for which to calculate the percentile ranks.</para>
	/// </summary>
	public PercentileRanksAggregationDescriptor<TDocument> Values(ICollection<double>? values)
	{
		ValuesValue = values;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		writer.WritePropertyName("percentile_ranks");
		writer.WriteStartObject();
		if (FieldValue is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, FieldValue, options);
		}

		if (!string.IsNullOrEmpty(FormatValue))
		{
			writer.WritePropertyName("format");
			writer.WriteStringValue(FormatValue);
		}

		if (HdrDescriptor is not null)
		{
			writer.WritePropertyName("hdr");
			JsonSerializer.Serialize(writer, HdrDescriptor, options);
		}
		else if (HdrDescriptorAction is not null)
		{
			writer.WritePropertyName("hdr");
			JsonSerializer.Serialize(writer, new HdrMethodDescriptor(HdrDescriptorAction), options);
		}
		else if (HdrValue is not null)
		{
			writer.WritePropertyName("hdr");
			JsonSerializer.Serialize(writer, HdrValue, options);
		}

		if (MissingValue is not null)
		{
			writer.WritePropertyName("missing");
			JsonSerializer.Serialize(writer, MissingValue, options);
		}

		if (ScriptValue is not null)
		{
			writer.WritePropertyName("script");
			JsonSerializer.Serialize(writer, ScriptValue, options);
		}

		if (TDigestDescriptor is not null)
		{
			writer.WritePropertyName("tdigest");
			JsonSerializer.Serialize(writer, TDigestDescriptor, options);
		}
		else if (TDigestDescriptorAction is not null)
		{
			writer.WritePropertyName("tdigest");
			JsonSerializer.Serialize(writer, new TDigestDescriptor(TDigestDescriptorAction), options);
		}
		else if (TDigestValue is not null)
		{
			writer.WritePropertyName("tdigest");
			JsonSerializer.Serialize(writer, TDigestValue, options);
		}

		if (ValuesValue is not null)
		{
			writer.WritePropertyName("values");
			JsonSerializer.Serialize(writer, ValuesValue, options);
		}

		writer.WriteEndObject();
		if (MetaValue is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, MetaValue, options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class PercentileRanksAggregationDescriptor : SerializableDescriptor<PercentileRanksAggregationDescriptor>
{
	internal PercentileRanksAggregationDescriptor(Action<PercentileRanksAggregationDescriptor> configure) => configure.Invoke(this);

	public PercentileRanksAggregationDescriptor() : base()
	{
	}

	private Elastic.Clients.Elasticsearch.Serverless.Field? FieldValue { get; set; }
	private string? FormatValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.HdrMethod? HdrValue { get; set; }
	private HdrMethodDescriptor HdrDescriptor { get; set; }
	private Action<HdrMethodDescriptor> HdrDescriptorAction { get; set; }
	private IDictionary<string, object>? MetaValue { get; set; }
	private FieldValue? MissingValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Script? ScriptValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.TDigest? TDigestValue { get; set; }
	private TDigestDescriptor TDigestDescriptor { get; set; }
	private Action<TDigestDescriptor> TDigestDescriptorAction { get; set; }
	private ICollection<double>? ValuesValue { get; set; }

	public PercentileRanksAggregationDescriptor Field(Elastic.Clients.Elasticsearch.Serverless.Field? field)
	{
		FieldValue = field;
		return Self;
	}

	public PercentileRanksAggregationDescriptor Field<TDocument, TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	public PercentileRanksAggregationDescriptor Field<TDocument>(Expression<Func<TDocument, object>> field)
	{
		FieldValue = field;
		return Self;
	}

	public PercentileRanksAggregationDescriptor Format(string? format)
	{
		FormatValue = format;
		return Self;
	}

	/// <summary>
	/// <para>Uses the alternative High Dynamic Range Histogram algorithm to calculate percentile ranks.</para>
	/// </summary>
	public PercentileRanksAggregationDescriptor Hdr(Elastic.Clients.Elasticsearch.Serverless.Aggregations.HdrMethod? hdr)
	{
		HdrDescriptor = null;
		HdrDescriptorAction = null;
		HdrValue = hdr;
		return Self;
	}

	public PercentileRanksAggregationDescriptor Hdr(HdrMethodDescriptor descriptor)
	{
		HdrValue = null;
		HdrDescriptorAction = null;
		HdrDescriptor = descriptor;
		return Self;
	}

	public PercentileRanksAggregationDescriptor Hdr(Action<HdrMethodDescriptor> configure)
	{
		HdrValue = null;
		HdrDescriptor = null;
		HdrDescriptorAction = configure;
		return Self;
	}

	public PercentileRanksAggregationDescriptor Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
	{
		MetaValue = selector?.Invoke(new FluentDictionary<string, object>());
		return Self;
	}

	public PercentileRanksAggregationDescriptor Missing(FieldValue? missing)
	{
		MissingValue = missing;
		return Self;
	}

	public PercentileRanksAggregationDescriptor Script(Elastic.Clients.Elasticsearch.Serverless.Script? script)
	{
		ScriptValue = script;
		return Self;
	}

	/// <summary>
	/// <para>Sets parameters for the default TDigest algorithm used to calculate percentile ranks.</para>
	/// </summary>
	public PercentileRanksAggregationDescriptor TDigest(Elastic.Clients.Elasticsearch.Serverless.Aggregations.TDigest? tDigest)
	{
		TDigestDescriptor = null;
		TDigestDescriptorAction = null;
		TDigestValue = tDigest;
		return Self;
	}

	public PercentileRanksAggregationDescriptor TDigest(TDigestDescriptor descriptor)
	{
		TDigestValue = null;
		TDigestDescriptorAction = null;
		TDigestDescriptor = descriptor;
		return Self;
	}

	public PercentileRanksAggregationDescriptor TDigest(Action<TDigestDescriptor> configure)
	{
		TDigestValue = null;
		TDigestDescriptor = null;
		TDigestDescriptorAction = configure;
		return Self;
	}

	/// <summary>
	/// <para>An array of values for which to calculate the percentile ranks.</para>
	/// </summary>
	public PercentileRanksAggregationDescriptor Values(ICollection<double>? values)
	{
		ValuesValue = values;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		writer.WritePropertyName("percentile_ranks");
		writer.WriteStartObject();
		if (FieldValue is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, FieldValue, options);
		}

		if (!string.IsNullOrEmpty(FormatValue))
		{
			writer.WritePropertyName("format");
			writer.WriteStringValue(FormatValue);
		}

		if (HdrDescriptor is not null)
		{
			writer.WritePropertyName("hdr");
			JsonSerializer.Serialize(writer, HdrDescriptor, options);
		}
		else if (HdrDescriptorAction is not null)
		{
			writer.WritePropertyName("hdr");
			JsonSerializer.Serialize(writer, new HdrMethodDescriptor(HdrDescriptorAction), options);
		}
		else if (HdrValue is not null)
		{
			writer.WritePropertyName("hdr");
			JsonSerializer.Serialize(writer, HdrValue, options);
		}

		if (MissingValue is not null)
		{
			writer.WritePropertyName("missing");
			JsonSerializer.Serialize(writer, MissingValue, options);
		}

		if (ScriptValue is not null)
		{
			writer.WritePropertyName("script");
			JsonSerializer.Serialize(writer, ScriptValue, options);
		}

		if (TDigestDescriptor is not null)
		{
			writer.WritePropertyName("tdigest");
			JsonSerializer.Serialize(writer, TDigestDescriptor, options);
		}
		else if (TDigestDescriptorAction is not null)
		{
			writer.WritePropertyName("tdigest");
			JsonSerializer.Serialize(writer, new TDigestDescriptor(TDigestDescriptorAction), options);
		}
		else if (TDigestValue is not null)
		{
			writer.WritePropertyName("tdigest");
			JsonSerializer.Serialize(writer, TDigestValue, options);
		}

		if (ValuesValue is not null)
		{
			writer.WritePropertyName("values");
			JsonSerializer.Serialize(writer, ValuesValue, options);
		}

		writer.WriteEndObject();
		if (MetaValue is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, MetaValue, options);
		}

		writer.WriteEndObject();
	}
}